package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.userms.dto.VkQuerySettingDTO;
import com.yuanfeng.userms.entity.UserVkSetEntity;
import com.yuanfeng.userms.mapper.UserVkSetMapper;
import com.yuanfeng.userms.service.UserVkSetService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName UserVkSetServiceImpl
 * @Description
 * @Author hyq
 * @Date 2023/7/14 10:22
 * @Version 1.0
 **/
@Service
public class UserVkSetServiceImpl extends ServiceImpl<UserVkSetMapper, UserVkSetEntity>
        implements UserVkSetService {
    @Override
    public ResponseResult enableVk(VkQuerySettingDTO dto) {
        //查询微客设置列表
        UserVkSetEntity result=this.baseMapper.selectOne(new LambdaQueryWrapper<UserVkSetEntity>());
        if(null == result){
            this.baseMapper.insertEnableVk(dto);
        }else{
            this.baseMapper.updateEnableVk(dto);
        }
        //查询分佣设置列表
        Map<String,Object> commissionSettingResult=this.baseMapper.commissionSetting();
        if(null == commissionSettingResult){
            this.baseMapper.insertVk(dto);
        }else {
            this.baseMapper.updateVk(dto);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult addOrUpdateVkQuerySetting(VkQuerySettingDTO param) {
        //后端判断微客名和微客标题名字
        String vkName = param.getVkName();
        String vkTitle = param.getVkTitle();
        if(!StringUtils.isBlank(vkName) && vkName.length()>=6){
            return ResponseResult.fail(0, "微客名称不得超过6个字符");
        }
        if(!StringUtils.isBlank(vkTitle) && vkTitle.length()>=8){
            return ResponseResult.fail(0, "顶部标题不得超过8个字符");
        }
        //查询微客设置表中的值
        UserVkSetEntity result=this.baseMapper.selectOne(new LambdaQueryWrapper<>());
        if(null==result){
            this.baseMapper.insertSetting(param);
        }else{
            this.baseMapper.updateSetting(param);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult vkQuerySetting() {
        //查询微客设置表中的值
        UserVkSetEntity result=this.baseMapper.selectOne(new LambdaQueryWrapper<UserVkSetEntity>());
        return ResponseResult.success(result);
    }
}
